package unitTests;

import static org.junit.Assert.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import mainpackage.Category;
import mainpackage.CategoryNameNotUniqueException;
import mainpackage.Color;
import mainpackage.DataManager;
import mainpackage.NoColorsLeftException;
import mainpackage.Todo;

import org.junit.Test;

import db.DBCreator;

public class DataManagerTest {
	@Test
	public void addCategoryDoesntAllowUniqueNames() throws Exception {
		DBCreator.createDB("db1.db");
		DataManager dm = new DataManager("db1.db");
		dm.addCategory("categoryName");
		try {
			dm.addCategory("categoryName");
			throw new Exception(); // would fail if got here
		} catch (CategoryNameNotUniqueException e) {
			// should catch it
		}
		dm.addCategory("anotherCategory");
		// assertEquals(true, true);
		// assertEquals(Color.YELLOW, Color.GREEN.nextColor());
	}

	@Test
	public void addCategoryDoesntAllowMoreCategoriesThanColors()
			throws Exception {
		DBCreator.createDB("db2.db");
		DataManager dm = new DataManager("db2.db");
		dm.addCategory("one");
		dm.addCategory("two");
		dm.addCategory("three");
		dm.addCategory("four");
		dm.addCategory("five");
		dm.addCategory("six");
		try {
			dm.addCategory("too many!!!");
			throw new Exception(); // would fail if got here
		} catch (NoColorsLeftException e) {
			// ok if got here
		}

		dm.removeCategory("three");
		dm.addCategory("nowEnoughColors");
	}

	@Test
	public void getTodosByDate() throws Exception {
		DBCreator.createDB("db3.db");
		DataManager dm = new DataManager("db3.db");
		Category c = dm.addCategory("some category");
		Todo todo = new Todo("one", "bla-bla", "2010/10/10 01:20",
				"2010/10/12 01:20", c);
		dm.addTodo(todo);
		todo = new Todo("two", "bla-bla", "2010/10/08 01:20",
				"2010/10/09 00:20", c);
		dm.addTodo(todo);
		todo = new Todo("three", "bla-bla", "2010/10/10 01:20",
				"2010/10/11 01:20", c);
		dm.addTodo(todo);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yy/MM/dd H:m");
		Date convertedStart = dateFormat.parse("2010/10/09 01:20");
		Date convertedEnd = dateFormat.parse("2010/10/11 01:20");
		List<Todo> todos = dm.getTodo(convertedStart, convertedEnd);
		assertEquals("one", todos.get(0).getName());
		assertEquals("three", todos.get(1).getName());
	}

	@Test
	public void getTodosSpecifiedCategories() throws Exception {
		DBCreator.createDB("db6.db");
		List<String> categories = new ArrayList<String>();
		DataManager dm = new DataManager("db6.db");
		Category c = dm.addCategory("Category 1");
		Category c2 = dm.addCategory("Category 2");
		Todo todo = new Todo("Event 1", "Description", "2012/10/16 08:00",
				"2012/11/16 08:00", c);
		dm.addTodo(todo);
		todo = new Todo("Event 2", "Description", "2012/10/08 00:00",
				"2012/10/16 00:00", c2);
		dm.addTodo(todo);
		todo = new Todo("Event 3", "Description", "2012/10/16 08:00",
				"2012/11/16 08:00", c2);
		todo = new Todo("Event 4", "Description", "2012/10/16 10:00",
				"2012/10/16 16:00", c);
		dm.addTodo(todo);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yy/MM/dd H:m");
		Date convertedStart = dateFormat.parse("2012/10/16 00:00");
		Date convertedEnd = dateFormat.parse("2012/11/16 00:00");
		categories.add("Category 1");
		List<Todo> todos = dm.getTodo(convertedStart, convertedEnd, categories);
		assertEquals("Event 1", todos.get(0).getName());
		assertEquals("Event 4", todos.get(1).getName());

	}

	@Test
	public void categoryByNameTest() throws Exception {
		DBCreator.createDB("db4.db");
		DataManager dm = new DataManager("db4.db");
		Category c = dm.addCategory("some category2");
		assertEquals(c, dm.getCategoryByName("some category2"));
		assertEquals(null, dm.getCategoryByName("does not exist"));
	}

	@Test
	public void editTodoTest() throws Exception {
		DBCreator.createDBWithDummyData("db5.db");
		DataManager dm = new DataManager("db5.db");
		dm.editTodo(dm.getAllTodos().get(0), "new name", "new desc",
				"2010/10/09 01:20", "2010/10/09 02:20",
				dm.getCategoryByName("Family"));
		assertEquals("new desc", dm.getAllTodos().get(0).getDescription());
	}

	@Test
	public void getAllTodosTest() throws Exception {
		DBCreator.createDBWithDummyData("db7.db");
		DataManager dm = new DataManager("db7.db");
		dm.getAllTodos();
		assertEquals("yay!!", dm.getAllTodos().get(0).getDescription());
	}

	@Test
	public void getTodoTest() throws Exception {
		DBCreator.createDBWithDummyData("db8.db");
		DataManager dm = new DataManager("db8.db");
		SimpleDateFormat dateFormat = new SimpleDateFormat("yy/MM/dd H:m");
		Date convertedStart = dateFormat.parse("2012/10/09 00:00");
		Date convertedEnd = dateFormat.parse("2012/11/10 00:00");
		dm.getTodo(convertedStart, convertedEnd);
		assertEquals("yay!!", dm.getAllTodos().get(0).getDescription());
	}

	@Test
	public void validateTest() throws Exception {
		DBCreator.createDBWithDummyData("db9.db");
		DataManager dm = new DataManager("db9.db");
		SimpleDateFormat dateFormat = new SimpleDateFormat("yy/MM/dd H:m");
		Date convertedStart = dateFormat.parse("2012/10/09 00:00");
		Date convertedEnd = dateFormat.parse("2012/11/10 00:00");
		assertEquals(true, dm.timeIsFree(convertedStart, convertedEnd));
	}

	@Test
	public void removeTodo() throws Exception {
		DBCreator.createDBWithDummyData("db10.db");
		DataManager dm = new DataManager("db10.db");
		SimpleDateFormat dateFormat = new SimpleDateFormat("yy/MM/dd H:m");
		Date convertedStart = dateFormat.parse("2012/10/09 00:00");
		Date convertedEnd = dateFormat.parse("2012/11/10 00:00");
		Todo toBeRemoved = new Todo("veryuniquename", "d", convertedStart,
				convertedEnd, dm.getCategoryByName("Work"));
		dm.addTodo(toBeRemoved);
		dm.removeTodo(toBeRemoved);
		List<Todo> todos = dm.getAllTodos();
		assertEquals(2, todos.size());
		for (Todo t : todos) {
			assertFalse(t.getName().equals("veryuniquename"));
		}
	}

	@Test
	public void getCategories() throws Exception {
		DBCreator.createDBWithDummyData("db11.db");
		DataManager dm = new DataManager("db11.db");
		List<Category> categories = dm.getCategories();
		assertEquals("Family", categories.get(0).getName());
		assertEquals("Work", categories.get(1).getName());
	}
	
	@Test
	public void getTodoById() throws Exception {
		DBCreator.createDB("db12.db");
		DataManager dm = new DataManager("db12.db");
		Category c = dm.addCategory("Category 1");
		Todo todo = new Todo("Event 1", "Description", "2012/10/16 08:00",
				"2012/11/16 08:00", c);
		dm.addTodo(todo);
		Todo todo2 = new Todo("Event 2", "Description", "2012/10/16 08:00",
				"2012/11/16 08:00", c);
		dm.addTodo(todo2);
	
		assertEquals("Event 1", dm.getTodoById(1).getName());
		assertEquals("Event 2", dm.getTodoById(2).getName());
	}
	
	@Test
	public void hasVacantColor() throws Exception {
		DBCreator.createDB("db13.db");
		DataManager dm = new DataManager("db13.db");
		Category c = dm.addCategory("Category 1");
		Category c2 = dm.addCategory("Category 2");
		Category c3 = dm.addCategory("Category 3");
		Category c4 = dm.addCategory("Category 4");
		assertEquals(true, dm.hasVacantColor());
		Category c5 = dm.addCategory("Category 5");
		Category c6 = dm.addCategory("Category 6");
		assertFalse(dm.hasVacantColor());
	}
	
}
